//
//  SaHttpRequest.m
//  saas
//
//  Created by fangyibao on 2019/9/9.
//  Copyright © 2019 fangmao. All rights reserved.
//

#import "SaHttpRequest.h"
#import "AFNetworking.h"

NSString *const Base_Url = @"http://test.sczhbf.com:58088";
//NSString *const Base_Url = @"https://inc-kzqtxz.caacsri.com";




NSInteger TimeOutInterval = 60;
@interface SaHttpRequest()
@property (nonatomic, strong) AFHTTPRequestSerializer *request;

@end
static SaHttpRequest *_request;
@implementation SaHttpRequest
+(instancetype)shareRequest{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _request = [self new];
    });
    return _request;
}
- (NSString *)checkUrl:(NSString *)url{
    if([url hasPrefix:@"http"]){
        return url;
    }
    return [NSString stringWithFormat:@"%@%@", Base_Url, url];
}
- (NSMutableURLRequest *)generateRequestWithUrlPath:(NSString *)urlPath useHttps:(BOOL)useHttps method:(NSString *)method params:(NSDictionary *)param{
    NSString *url = [self checkUrl:urlPath];
    NSString *m = [method uppercaseString];
    NSMutableURLRequest *req = [self.request requestWithMethod:m URLString:url parameters:param error:nil];
    req.timeoutInterval = TimeOutInterval;
    return req;
}

- (NSMutableURLRequest *)generateUploadRequestWithUrl:(NSString *)urlPath method:(NSString *)method souceImg:(UIImage *)img params:(NSDictionary *)param{
    NSString *url = [self checkUrl:urlPath];
    NSMutableURLRequest *req = [self.request multipartFormRequestWithMethod:@"POST" URLString:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        [formData appendPartWithFileData:UIImagePNGRepresentation(img) name:@"file" fileName:param[@"filename"][0] mimeType:@"image/jpeg"];
        [formData appendPartWithFileData:UIImagePNGRepresentation(img) name:@"file" fileName:@"照片" mimeType:@"image/jpeg"];

    } error:nil];
    req.timeoutInterval = TimeOutInterval * 2;
    return req;
}
- (NSMutableURLRequest *)generateUploadRequestWithUrl:(NSString *)urlPath method:(NSString *)method imgs:(NSArray *)imgs params:(NSDictionary *)param{
    NSString *url = [self checkUrl:urlPath];
    
    NSMutableURLRequest *req = [self.request multipartFormRequestWithMethod:@"POST" URLString:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        int index = 0;
        for (id img in imgs) {
            if ([img isKindOfClass:[UIImage class]]) [formData appendPartWithFileData:UIImagePNGRepresentation(img) name:[NSString stringWithFormat:@"file %@", @(index)] fileName:@".jpg" mimeType:@"image/jpeg"];
            if ([img isKindOfClass:[NSDictionary class]]){
                NSString *key = ((NSDictionary *)img).allKeys.firstObject;
                [formData appendPartWithFileData:UIImagePNGRepresentation(img[key]) name:key fileName:@".jpg" mimeType:@"image/jpeg"];
            }
            
            index ++;
        }
        
    } error:nil];
    req.timeoutInterval = TimeOutInterval * 2;
    return req;
}
- (AFHTTPRequestSerializer *)request{
    if (!_request) {
        _request = [AFJSONRequestSerializer serializer];
        [_request setValue:@"1" forHTTPHeaderField:@"Tenant-Id"]; // 请求头
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [_request setValue:[defaults valueForKey:@"accessToken"] forHTTPHeaderField:@"Authorization"];
    }
    return _request;
}

@end
